Javascript Lesson 1: Hello World

Typically, a "Hello World" program is the most basic program you can create for a language. The purpose of the program is to print the worlds "Hello World" onto the screen.

Below this paragraph, you'll find a small module that Prints "Hello World" to the screen.
There is also source code with an explanation for each line.


-Line 1 has the creation of a button with the "< button >" tag.
On the inside of the first button tag, there is an event trigger labeled "onclick".

As their name implies, event triggers are statements that initiate when a specified event occurs. This one is triggered by the user clicking on the button. The "onclick" event is followed by a reference to our function "myFunction()", which houses our javascript code.

-Line 3 creates a < h2 > element with the id "demo" for demonstration. This h2 element is given an id so it can be used later within the code.

-Line 5 is the beginning of our < script > tag, which is what allows us to place javascript code in our website.

-Line 7 creates a function named "myFunction" that contains our JavaScript code

-Line 9 contains the statement " document.getElementById ".
The " getElementById() " function selects an html element with a specified id; in this case, we are selecting the h2 element labeled "demo". The next part of the function ".innerHTML = Hello World" changes the text of our header to the words "Hello World".

Copy and paste the source code into the body of your website to use this program.